package com.ywjt.pinkelephant.my;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.activity.result.ActivityResultCaller;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.stx.xhb.xbanner.XBanner;
import com.ywjt.pinkelephant.R;
import com.ywjt.pinkelephant.base.BaseVPFragment;
import com.ywjt.pinkelephant.my.activity.ActivityCodeChange;
import com.ywjt.pinkelephant.my.activity.ActivityExtendCenter;
import com.ywjt.pinkelephant.my.activity.ActivityTeamCarryOut;
import com.ywjt.pinkelephant.my.activity.ActivityVipCenter;
import com.ywjt.pinkelephant.my.activity.ActivityShare;
import com.ywjt.pinkelephant.my.adapter.MyMidAdapter;
import com.ywjt.pinkelephant.my.model.MyMidModel;

import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.Result;

import static android.app.Activity.RESULT_OK;


public class MyIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private View view;
    private RecyclerView rcCommon;
    private List<MyMidModel> data;
    private MyMidAdapter myMidAdapter;
    private XBanner banner;
    private List<Integer> banners;
    private LinearLayout llShare,llCodeChange,llExtendCenter,llTeamCarryOut;
    private TextView tvOpenVip;
    private ImageView ivMsg;
    private TextView tvCopy;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llShare:
                //分享给朋友
                ActivityShare.actionStart(context);
                break;
            case R.id.tvOpenVip:
                //开通vip
                ActivityVipCenter.actionStart(context);
                break;
            case R.id.llCodeChange:
                //分享兑换码
                ActivityCodeChange.actionStart(context);
                break;
            case R.id.llExtendCenter:
                //推广中心
                ActivityExtendCenter.actionStart(context);
                break;
            case R.id.llTeamCarryOut:
                //团队转账
                ActivityTeamCarryOut.actionStart(context);
                break;
            case R.id.ivMsg:
                Intent intent = new Intent();
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
                intent.setAction(Intent.ACTION_PICK);
                intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //直接打开系统相册  不设置会有选择相册一步（例：系统相册、QQ浏览器相册）
                //intent.setData(MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(Intent.createChooser(intent,"选择照片"),200);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            if (requestCode==200){
                ClipData imageNames = data.getClipData();
                if (imageNames != null){
                    Glide.with(context).load(imageNames.getItemAt(0).getUri()).into(ivMsg) ;
                }
            }
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() { }

    public static MyIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        MyIndex fl = new MyIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.my_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        rcCommon=view.findViewById(R.id.rcCommon);
        banner=view.findViewById(R.id.banner);
        llShare=view.findViewById(R.id.llShare);
        ivMsg=view.findViewById(R.id.ivMsg);
        tvCopy=view.findViewById(R.id.tvCopy);
        llCodeChange=view.findViewById(R.id.llCodeChange);
        llExtendCenter=view.findViewById(R.id.llExtendCenter);
        tvOpenVip=view.findViewById(R.id.tvOpenVip);
        llTeamCarryOut=view.findViewById(R.id.llTeamCarryOut);

        tvCopy.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线
        tvCopy.getPaint().setAntiAlias(true);//抗锯齿

        data=new ArrayList<>();
        MyMidModel myMidModel=new MyMidModel();
        myMidModel.setTitle("我的收益");
        myMidModel.setMoney1text("可提现金额");
        myMidModel.setMoney2text("今日收益");
        myMidModel.setMoney1value("¥0.00");
        myMidModel.setMoney2value("¥0.00");
        data.add(myMidModel);
        MyMidModel myMidModel1=new MyMidModel();
        myMidModel1.setTitle("我的团队");
        myMidModel1.setMoney1text("全部达人");
        myMidModel1.setMoney2text("新增直接达人");
        myMidModel1.setMoney1value("0");
        myMidModel1.setMoney2value("0");
        data.add(myMidModel1);

        myMidAdapter=new MyMidAdapter(data);
        rcCommon.setAdapter(myMidAdapter);
        rcCommon.setLayoutManager(new GridLayoutManager(context,2,RecyclerView.VERTICAL,false));

        banners=new ArrayList<>();
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banner.setData(R.layout.item_banner1,banners,null);
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object object, View view, int position) {
                RelativeLayout container=(RelativeLayout) view;
                ImageView ivBanner=container.findViewById(R.id.ivBanner);
                Glide.with(context).
                        load(banners.get(position)).into(ivBanner);
            }
        });

        llShare.setOnClickListener(this::onClick);
        tvOpenVip.setOnClickListener(this::onClick);
        llCodeChange.setOnClickListener(this::onClick);
        llExtendCenter.setOnClickListener(this::onClick);
        llTeamCarryOut.setOnClickListener(this::onClick);
        tvCopy.setOnClickListener(this::onClick);
        ivMsg.setOnClickListener(this::onClick);
    }
}